package com.cheems.cheemsspringaialibaba.Graph.node;

import com.alibaba.cloud.ai.graph.NodeOutput;
import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.action.NodeAction;
import com.cheems.cheemsspringaialibaba.Tools.OpenMeteoService;
import com.cheems.cheemsspringaialibaba.Tools.SearchFromDify;
import org.bsc.async.AsyncGenerator;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.model.ChatResponse;
import reactor.core.publisher.Flux;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.cheems.cheemsspringaialibaba.Graph.config.HenauRoutingGraphConfiguration.availableRoutes;

/**
 * @author JTB
 */
public class NormalNode implements NodeAction{


        private final ChatClient client;

        public NormalNode(ChatClient client) {
            this.client = client;
        }

        @Override
        public Map<String, Object> apply(OverAllState state) {
            String inputText = (String) state.value("inputText").orElse("");
            String selectionLlm = (String) state.value("selectionLlm").orElse("");

            // 创建流式响应
            AssistantMessage result = client.prompt()
                    .system(availableRoutes.get(selectionLlm).toString())
                    .user(inputText)
                    .tools()
                    .call()
                    .chatResponse()
                    .getResult()
                    .getOutput();


            return Map.of("result", result);
        }

}
